home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Sample.bin / Click2.java < prev    next >
Text File  |  1998-09-15  |  3KB  |  99 lines

  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.applet.*;
  5.  
  6.  
  7. /** 
  8.  * This version of the Click applet fixes Click2 by extending
  9.  * the nested TargetListener class with methods that redispatch
  10.  * the mouse motion events from the target columns to the applet.
  11.  * Note that we're translating the events coordinates from the
  12.  * source components coordinate system to the applets coordinate
  13.  * system. 
  14.  * 
  15.  * This applet runs correctly in HotJava, it requires JDK 1.1.
  16.  */
  17.  
  18. public class Click2 extends Applet
  19. {
  20.   Color puckColor = new Color(200, 0, 10);
  21.   Box puck = new Box(puckColor);
  22.   ColumnOfBoxes[] targets = new ColumnOfBoxes[8];
  23.  
  24.   private final static class TargetListener 
  25.     extends MouseAdapter implements MouseMotionListener
  26.   {
  27.     private Color newBackground;
  28.     private Color oldBackground;
  29.  
  30.     TargetListener(Color newBackground) {
  31.       this.newBackground = newBackground;
  32.     }
  33.  
  34.     public void mouseEntered(MouseEvent e) {
  35.       oldBackground = e.getComponent().getBackground();
  36.       e.getComponent().setBackground(newBackground);
  37.     }
  38.  
  39.     public void mouseExited(MouseEvent e) {
  40.       e.getComponent().setBackground(oldBackground);
  41.     }
  42.  
  43.     private void redispatch(MouseEvent e) {
  44.       Point origin = e.getComponent().getLocation();
  45.       e.translatePoint(origin.x, origin.y);
  46.       e.getComponent().getParent().dispatchEvent(e);
  47.     }
  48.  
  49.     public void mouseMoved(MouseEvent e) {  redispatch(e); }
  50.     public void mouseDragged(MouseEvent e) { redispatch(e); }
  51.     public void mouseClicked(MouseEvent e) { redispatch(e); }
  52.   }
  53.  
  54.   public Click2()
  55.   {
  56.     MouseMotionListener movePuck = new MouseMotionAdapter() {
  57.       public void mouseMoved(MouseEvent e)
  58.       {
  59.     int x = e.getX();
  60.     int y = getSize().height - puck.getSize().height;
  61.     puck.setLocation(x, y);
  62.       }
  63.     };
  64.  
  65.     /* Create a row of targets, i.e. columns of boxes, along
  66.      * the top of the applet.  Each target column contains
  67.      * between one and four boxes.
  68.      */
  69.  
  70.     for(int i = 0; i < targets.length; i++) {
  71.       int nBoxes = 1 + (int)(Math.random() * 4.0);
  72.       float boxHue = (float)i / (float)targets.length;
  73.       Color boxColor = Color.getHSBColor(boxHue, 0.5f, 0.85f);
  74.       TargetListener tl = new TargetListener(boxColor.brighter());
  75.       targets[i] = new ColumnOfBoxes(boxColor, nBoxes);
  76.       targets[i].addMouseListener(tl);
  77.       targets[i].addMouseMotionListener(tl);
  78.       add(targets[i]);
  79.     }
  80.  
  81.     add(puck);
  82.     addMouseMotionListener(movePuck);
  83.   }
  84.  
  85.   public static void main(String[] args)
  86.   {
  87.     WindowListener l = new WindowAdapter()
  88.       {
  89.     public void windowClosing(WindowEvent e) {System.exit(0);}
  90.       };
  91.  
  92.     Frame f = new Frame("Click");
  93.     f.addWindowListener(l); 
  94.     f.add(new Click2());
  95.     f.setSize(600, 400);
  96.     f.show();
  97.   }
  98. }
  99.